import QtQuick 1.1
//import com.nokia.symbian 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1
import "platform.js" as PLATFORM
import NumismatMobile 1.0

Page {
    id: downloadDialog

    property string source: 'http://open-numismat-mobile.googlecode.com/files/list.xml'

    signal accepted(string fileName)

    onStatusChanged: {
        if (status == PageStatus.Active || status == PageStatus.Activating) {
            collectionModel.source = source;
            statusBar.text = qsTr("Catalogues")
        }
        else {
            collectionModel.source = '';
        }
    }

    Rectangle {
        anchors.fill: parent
        color: visual.colorBackgrond
    }

    XmlListModel {
        id: collectionModel
        query: "/collections/collection"

        XmlRole { name: "title"; query: "title/string()" }
        XmlRole { name: "pubDate"; query: "pubDate/string()" }
        XmlRole { name: "size"; query: "size/string()" }
        XmlRole { name: "file"; query: "file/string()" }
        XmlRole { name: "url"; query: "url/string()" }

        onStatusChanged: {
            if (status == XmlListModel.Error)
                collectionModelError.show();
        }
    }

    InfoBanner {
        id: collectionModelError
        anchors.bottom: parent.bottom
        text: qsTr("Can't open collections list ") + collectionModel.errorString();
    }

    Component {
        id: fileListDelegate

        DownloadListItem {
            id: listItem
            titleText: title
            subtitleText: file + ", " + size + ", " + pubDate

            function startDownload() {
                downloader.downloadFile(url, file);
            }

            onClicked: {
                if (downloader.checkFile(file)) {
                    overwriteConfirmation.accepted.connect(startDownload)
                    overwriteConfirmation.open();
                }
                else {
                    startDownload();
                }
            }
        }
    }

    ListView {
        id: fileList
        width: parent.width
        anchors.top: PLATFORM.TYPE == "harmattan" ? viewHeader.bottom : parent.top
        anchors.bottom: parent.bottom
        currentIndex: -1
        visible: !indicator.running

        delegate: fileListDelegate
        model: collectionModel
        highlightMoveSpeed: -1
    }

    DownloadFile {
        id: downloader
        onStatusChanged: {
            if (status == DownloadFile.Ready) {
                pageStack.pop();
                accepted(file);
            }
            else if (status == DownloadFile.Error) {
                downloaderError.show();
            }
        }
    }

    InfoBanner {
        id: downloaderError
        anchors.bottom: parent.bottom
        text: qsTr("Can't download collection ") + downloader.errorString();
    }

    BusyIndicator {
        id: indicator
        anchors.centerIn: parent
        visible: running
        running: (collectionModel.status == XmlListModel.Loading) || (downloader.status == DownloadFile.Loading)
        Component.onCompleted: {
            if (PLATFORM.TYPE == "symbian") {
                width = 96;
                height = 96;
            }
            else {
                platformStyle.size = "large";
            }
        }
    }

    Dialog {
        id: overwriteConfirmation
        property string file

        title: Text {
            width: parent.width
            font.pixelSize: visual.fontLarge
            font.bold: true
            horizontalAlignment: Text.AlignHCenter
            color: visual.colorInvertedForeground
            text: qsTr("File exists")
        }

        content: Text {
            width: parent.width
            font.pixelSize: visual.fontDefault
            horizontalAlignment: Text.AlignHCenter
            color: visual.colorInvertedForeground
            text: qsTr("File already exists. Overwrite?")
            wrapMode: Text.Wrap
        }

        buttons: ButtonColumn {
            anchors.horizontalCenter: parent.horizontalCenter
            spacing: 8

            Button {text: qsTr("Yes"); onClicked: overwriteConfirmation.accept()}
            Button {text: qsTr("Cancel"); onClicked: overwriteConfirmation.reject()}
        }
    }

    tools: ToolBarLayout {
        ToolIcon {
            platformIconId: "toolbar-back"
            onClicked: pageStack.pop()
        }
    }

    ViewHeader {
        id: viewHeader
        visible: PLATFORM.TYPE == "harmattan"
        clickable: false
        content: Item {
            anchors.fill: parent
            Text {
                text: qsTr("Catalogues")
                anchors.leftMargin: 5
                anchors.rightMargin: 5
                anchors.verticalCenter: parent.verticalCenter
                anchors.right: parent.right
                anchors.left: parent.left
                elide: Text.ElideRight
                font.pixelSize: visual.fontXlarge
                color: visual.colorInvertedForeground
            }
        }
    }
}
